package com.gexin.controller.weixin.subscribe;


import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.interceptor.XAppSafeInterceptor;
import com.gexin.core.web.AppController;
import com.gexin.model.weixin.WeixinAppConfig;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.wxaapp.WxaConfigKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;

/**
 * 小程序订阅消息
 *
 * @author 肖得明
 */
@Before(XAppSafeInterceptor.class)//微信授权拦截器
@ControllerBind(controllerKey = "/xapp/subscribe")
public class XAppSubscribeController extends AppController {
	private static Logger log = LogManager.getLogger(XAppSubscribeController.class);
	// 微信用户接口api
	protected WxaSubscribeMessageApi wxaUserApi = Duang.duang(WxaSubscribeMessageApi.class);

	//前端调用这个方法来实现订阅
	public void addEvent() {
		String subscribe_ids = getPara("subscribe_ids", null);
		if (StringUtil.isBlank(subscribe_ids)) {
			fail("ID 不能为空！");
			return;
		}
		WeixinUser user=getAuthUser();
		if(user==null){
			fail("请先进行授权管理");
			return;
		}
		String sp[] = subscribe_ids.split(",");
		for (String str : sp) {
			SubscribeMessageKit.api.addSubscribe(user.getId(), str);
		}
		success("订阅成功");
	}


	//推送审核结果消息
	public void PushAgentReviewMsg(){
		WxaConfigKit.setWxaConfig(WeixinAppConfig.dao.findById(2).getWxaConfigs());//设置对应的微信小程序
		WeixinUser user = getAuthUser();
		Boolean isOk = SubscribeMessageKit.api.subSubscribe(user.getId(), SubscribeMessageKit.Subscribe_Agent_Review_Message);
		if (!isOk) {
			fail("不能推送消息，没有订阅消息");
			return;
		}
		WxaSubscribeMessage t = new WxaSubscribeMessage();
		t.add("phrase1", "黎某人测试");//字段1
		t.add("thing2", "我存在测试");//字段2
		t.add("thing3", "签到即可领取五积分");//字段3
		t.add("name4", "签到即可领取五积分");//字段4
		t.add("date5", "2019-12-31 16:17:05");//字段5
		t.setTemplate_id(SubscribeMessageKit.Subscribe_Agent_Review_Message);//模板ID
		t.setTouser(getAuthUser().getOpenid());//推送的用户的openid
		t.setPage("pages/my/my?id" + IdGenerateUtils.getId());//打开的页面
		WxaSubscribeMessageApi api = new WxaSubscribeMessageApi();
		ApiResult resutl = api.send(t);
		if (!resutl.isSucceed()) {//推送失败
			System.err.println("代理商审核推送消息失败了！resutl："+JsonKit.toJson(resutl));
		}
	}

	//测试推送模板消息
	public void testPushMsg() {
		WxaConfigKit.setWxaConfig(WeixinAppConfig.dao.findById(2).getWxaConfigs());//设置对应的微信小程序
		WeixinUser user = getAuthUser();
		Boolean isOk = SubscribeMessageKit.api.subSubscribe(user.getId(), SubscribeMessageKit.Subscribe_Test_Message);
		if (!isOk) {
			fail("不能推送消息，没有订阅消息");
			return;
		}
		WxaSubscribeMessage t = new WxaSubscribeMessage();
		t.add("thing1", "xxxxx");//字段1  具体的字段自行参考模板消息
		t.add("number3", "1");//字段2
		t.add("thing4", "签到即可领取五积分");//字段3
		t.setTemplate_id(SubscribeMessageKit.Subscribe_Test_Message);//模板ID
		t.setTouser(getAuthUser().getOpenid());//推送的用户的openid
		t.setPage("pages/active/active?id" + IdGenerateUtils.getId());//打开的页面
		WxaSubscribeMessageApi api = new WxaSubscribeMessageApi();
		ApiResult resutl = api.send(t);
		if (resutl.isSucceed()) {
			success();
			return;
		}
		fail(resutl.getErrorMsg());//推送失败
	}

}
